<%= javascript_include_tag "application" %>

<% form_for(@document) do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :document_type %><br />
    <%= f.select :document_type, %w(MM WZ PZ), :include_blank => true %>
  </p>
  <p>
    <%= f.label :number %><br />
    <%= f.text_field :number, :size => 15 %>
  </p>
  <p>
    <%= f.label :source_id %><br />
    <%= f.select(:source_id, Transactor.find(:all).collect {|t| [ t.name, t.id ] }, { :include_blank => true }) %>
  </p>
  <p>
    <%= f.label :target_id %><br />
    <%= f.select(:target_id, Transactor.find(:all).collect {|t| [ t.name, t.id ] }, { :include_blank => true }) %>
  </p>
  <p>
    <%= f.label :notes %><br />
    <%= f.text_area :notes, :rows => 2, :cols => 25 %>
  </p>
  
  <% @employee = Employee.find(:first, :conditions => ["login = ?",session['employee']]) %>
  <%= f.hidden_field(:employee_id, :value => @employee.id)%>
<% end %>

<script type="text/javascript">
var positions = new Array();
var indexKey = 1;
</script>

<p>
Positions | <a href="#" onclick="{removeAllPositions(positions);}">Remove all</a>
</p>

<ul id="positions_list">
</ul>

<!-- prepopulate positions list -->
<script type="text/javascript">
<% @document.positions.each_with_index do |position, index_key| %>
  <% product = Product.find(position.product_id) %>
  addPosition(positions, <%= index_key + 1 %>, <%= position.product_id %>, "<%= product.name %>", "<%= position.quantity %>");
<% end %>
indexKey = positions.length + 1;
</script>


<form name="add_position_form" method="post">
  <%= label :position, :product_id, "Product" %>
  <%= select("position", "product_id", Product.find(:all).collect {|p| [ p.name, p.id ] }) %>

  <%= label :position, :quantity %>
  <%= text_field "position", "quantity", :size => 5 %>

  <input type="button" value="Add position" onclick = "
    if (validateAddPositionForm()) {
      addPositionFromForm(positions, indexKey);
	  indexKey++;
	}
  "/>
</form>

<p>
  <input type="button" value="Save" onclick="{
	setPositionsCookie(positions);
    document.forms[0].submit();
  }" />
</p>